package com.xyoto.community.service.impl;

import com.xyoto.community.constant.DeleteStatus;
import com.xyoto.community.constant.FollowStatus;
import com.xyoto.community.constant.ModuleType;
import com.xyoto.community.dto.RemoveFanFollowDTO;
import com.xyoto.community.dto.SaveFanFollowDTO;
import com.xyoto.community.entity.main.FanFollow;
import com.xyoto.community.enums.FollowEnum;
import com.xyoto.community.enums.UserEnum;
import com.xyoto.community.exception.ExceptionUtils;
import com.xyoto.community.mapper.main.FanFollowMapper;
import com.xyoto.community.mapper.main.MessageMapper;
import com.xyoto.community.mapper.main.UserMapper;
import com.xyoto.community.service.FanFollowService;
import com.xyoto.community.service.MessageService;
import com.xyoto.community.service.UserCacheService;
import com.xyoto.community.utils.JwtUtils;
import com.xyoto.community.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 粉丝关注实现类
 */
@Service
@Slf4j
public class FanFollowServiceImpl implements FanFollowService {

    @Autowired
    private FanFollowMapper fanFollowMapper;

    @Resource
    private HttpServletRequest request;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private MessageService messageService;

    @Autowired
    private FanFollowRedisImpl fanFollowRedis;

    @Autowired
    private MessageMapper messageMapper;

    @Autowired
    private UserCacheService userCacheService;

    /**
     * 关注用户
     * @param saveFanFollowDTO
     * @return
     */
    @Override
    @Transactional
    public Result saveFollow(SaveFanFollowDTO saveFanFollowDTO) {

        Integer userId = JwtUtils.getMemberIdByJwtToken(request);
        int targetId = saveFanFollowDTO.getTargetId();

        log.info("开始执行关注用户校验功能");
        //查询是否有该用户
        Integer userDeleteStatus = userMapper.getUserDeleteStatusById(targetId);
        if (userDeleteStatus == null || userDeleteStatus == DeleteStatus.YES){
            ExceptionUtils.error(UserEnum.USER_NOT_EXIST);
        }

        //查询是否已经关注过该用户
        FanFollow userFollowStatus = fanFollowMapper.getUserFollowStatus(userId,targetId);
        if (userFollowStatus != null && userFollowStatus.getFollowStatus() == FollowStatus.FOLLOW){
            ExceptionUtils.error(FollowEnum.FOLLOW_USER_YES);
        }

        //判断传过来的userId和targetId
        if (userId.equals(targetId)) {
            ExceptionUtils.error(FollowEnum.FOLLOW_USER_ME);
        }

        log.info("开始执行关注用户功能");
        FanFollow fanFollow = new FanFollow();
        fanFollow.setFollowStatus(FollowStatus.FOLLOW);
        fanFollow.setTargetId(targetId);
        fanFollow.setUserId(userId);

        log.info("开始执行关注用户SQL");
        if (userFollowStatus != null) {

            Integer updateFollow = fanFollowMapper.updateFollow(fanFollow);
            if (updateFollow == null || updateFollow == 0) {
                ExceptionUtils.error(FollowEnum.FOLLOW_CREATE_FAIL);
            }
            //开始存入消息
            messageService.saveMessage(userId,targetId,userFollowStatus.getId(), ModuleType.USER_FOLLOW,null,null,null,null);

        } else {
            Integer saveFollow = fanFollowMapper.saveFollow(fanFollow);
            if (saveFollow == null || saveFollow == 0) {
                ExceptionUtils.error(FollowEnum.FOLLOW_CREATE_FAIL);
            }
            //开始存入消息
            messageService.saveMessage(userId,targetId,fanFollow.getId(), ModuleType.USER_FOLLOW,null,null,null,null);
        }
        log.info("关注信息开始存入redis");
        userCacheService.addUserFollowStatus(userId,targetId);

        return Result.success();
    }

    /**
     * 取消关注用户
     * @param removeFanFollowDTO
     * @return
     */
    @Override
    @Transactional
    public Result removeFollow(RemoveFanFollowDTO removeFanFollowDTO) {

        Integer userId = JwtUtils.getMemberIdByJwtToken(request);


        int targetId = removeFanFollowDTO.getTargetId();

        log.info("开始执行取消关注用户校验");
        //查询是否有该用户
        Integer userDeleteStatus = userMapper.getUserDeleteStatusById(targetId);
        if (userDeleteStatus == null || userDeleteStatus == DeleteStatus.YES){
            ExceptionUtils.error(UserEnum.USER_NOT_EXIST);
        }

        //查询是否已经取消关注过该用户
        FanFollow userFollowStatus = fanFollowMapper.getUserFollowStatus(userId,targetId);
        if (userFollowStatus == null){
            ExceptionUtils.error(FollowEnum.FOLLOW_CANCEL_FAIL);
        }else if (userFollowStatus.getFollowStatus() == FollowStatus.UN_FOLLOW){
            ExceptionUtils.error(FollowEnum.FOLLOW_USER_NO);
        }
        log.info("开始执行取消关注用户功能");
        //若无上述条件则执行取消关注用户功能
        FanFollow fanFollow = new FanFollow();
        fanFollow.setFollowStatus(FollowStatus.UN_FOLLOW);
        fanFollow.setTargetId(targetId);
        fanFollow.setUserId(userId);

        log.info("开始执行取消关注用户SQL");
        Integer removeFollow = fanFollowMapper.removeFollow(fanFollow);
        if (removeFollow == null || removeFollow == 0){
            ExceptionUtils.error(FollowEnum.FOLLOW_USER_FAIL);
        }
        //删除消息表
        messageService.deleteMessage(userId,userFollowStatus.getId(),ModuleType.USER_FOLLOW);
        log.info("开始删除关注redis中关注状态");
        userCacheService.cancelUserFollow(userId,targetId);

        return Result.success();
    }
}
